package com.example.harmonyos_ui.slice;

import com.example.harmonyos_ui.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;


public class RedHeartSlice extends AbilitySlice {
    private Image red_heart = null;
    private Text red_heart_text = null;
    private boolean is_lighted = false;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_red_heart);
        // 获取图片
        red_heart = (Image)findComponentById(ResourceTable.Id_red_heart);
        // 获取文字
        red_heart_text = (Text)findComponentById(ResourceTable.Id_red_heart_text);
        // 获取整个页面
        DirectionalLayout red_heart_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_red_heart_layout);
        // 页面绑定事件
        red_heart_layout.setDoubleClickedListener(this::switchHeart);
    }

    // 改变图片状态
    private void switchHeart(Component component) {
        if (is_lighted) {
            red_heart.setImageAndDecodeBounds(ResourceTable.Media_white);
            red_heart_text.setText("双击喜欢");
            is_lighted = !is_lighted;
        } else {
            red_heart.setImageAndDecodeBounds(ResourceTable.Media_red);            red_heart_text.setText("双击喜欢");
            red_heart_text.setText("双击取消");
            is_lighted = !is_lighted;
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
